<template>
	<view class="container">
		<view class="tui-page__hd">
			<view class="tui-page__title">Digital Keyboard</view>
			<view class="tui-page__desc">数字键盘，可自定义颜色，按钮文字等</view>
		</view>
		<view class="tui-page__bd">
			<view class="tui-form__box">
				<view class="tui-title">转账金额</view>
				<view class="tui-input__box" @tap="focus">
					<view class="tui-symbol">￥</view>
					<view class="tui-input">{{value}}</view>
					<view class="tui-cursor" v-if="cursor"></view>
				</view>
			</view>
			<tui-button type="gray-primary" :size="34" bold width="372rpx" height="84rpx" margin="80rpx auto 40rpx" @click="changeStyle">换按钮背景色</tui-button>
			<tui-button type="gray-primary" :size="34" bold width="372rpx" height="84rpx" margin="40rpx auto" @click="changeMode">暗黑模式</tui-button>
			<tui-button type="gray-primary" :size="34" bold width="372rpx" height="84rpx" margin="0 auto" @click="disabledDecimal">去掉小数点</tui-button>
			<tui-digital-keyboard :buttonBackground="buttonBackground" :buttonText="text" :darkMode="darkMode" :show="show"
			 :disabled="value==''" :isDecimal="isDecimal" @click="keyTap" @backspace="backspace" @confirm="confirm"></tui-digital-keyboard>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				show: false,
				value: '',
				cursor: false,
				text: '转账',
				buttonBackground: '#5677fc',
				darkMode: false,
				isDecimal: true
			};
		},
		methods: {
			focus() {
				this.isDecimal = true;
				this.darkMode = false;
				this.buttonBackground = '#5677fc';
				this.text = '转账';
				this.cursor = true;
				this.show = true;
			},
			changeStyle() {
				this.isDecimal = true;
				this.text = '确定';
				this.cursor = true;
				this.darkMode = false;
				this.buttonBackground = '#07c160';
				this.show = true;
			},
			changeMode() {
				this.isDecimal = true;
				this.text = '确定';
				this.cursor = true;
				this.darkMode = true;
				this.show = true;
			},
			disabledDecimal() {
				this.isDecimal = false;
				this.cursor = true;
				this.show = true;
			},
			keyTap(e) {
				console.log(e)
				let keyVal = e.value;
				let value = this.value;
				if (~value.indexOf('.') && keyVal == '.') return;
				//最大9位，自行控制即可
				if (value.length == 9) {
					this.tui.toast('超过限制位数，不可输入')
					return
				}
				if (!this.value && keyVal == '.') {
					this.value = '0.'
				} else {
					this.value = this.value + e.value;
				}
			},
			backspace(e) {
				let val = this.value;
				if (val) {
					this.value = val.substring(0, val.length - 1)
				}
			},
			confirm(e) {
				this.show = false;
				this.cursor = false;
				this.tui.toast('转账金额为：' + Number(this.value))
			}
		}
	};
</script>

<style>
	.tui-form__box {
		background-color: #fff;
		padding: 0 50rpx 60rpx;
		box-sizing: border-box;
	}

	.tui-title {
		padding: 60rpx 0;
		box-sizing: border-box;
		font-size: 28rpx;
	}

	.tui-input__box {
		display: flex;
		align-items: center;
		border-bottom: 1rpx solid rgba(0, 0, 0, .1);
		height: 88rpx;
		padding-bottom: 2rpx;
	}

	.tui-symbol {
		font-size: 60rpx;
		font-weight: bold;
	}

	.tui-input {
		font-size: 80rpx;
		line-height: 80rpx;
		font-weight: bold;
		padding-right: 6rpx;
	}

	.tui-cursor {
		display: block;
		width: 2px;
		border-radius: 2px;
		height: 88rpx;
		animation: blink 1s infinite steps(1, start)
	}

	@keyframes blink {
		0% {
			background-color: white;
		}

		50% {
			background-color: #5677fc;
		}

		100% {
			background-color: white;
		}
	}
</style>
